﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PanhuaWin.工厂.车间.计划
{
    public partial class Form计划查询 :   UIPage
    {
        private UIRadioButton 当前来源选项 = null;
        public Form计划查询()
        {
            InitializeComponent();
        }

        private void Form计划查询_Load(object sender, EventArgs e)
        {
            this.uiDatePicker开始日期.Value = DateTime.Today.AddDays(-1);
            this.uiDatePicker结束日期.Value = DateTime.Today;
            uiDataGridView已选.AutoGenerateColumns = false;
        }

        private void uiComboBox产线_SelectedIndexChanged(object sender, EventArgs e)
        {
            pub.ListWorkTypeAll(pub.全局工厂编号, this.uiComboBox产线.SelectedValue.ToString(), this.uiComboBox去向);

        }

        private void uiButton查询_Click(object sender, EventArgs e)
        {
            query();
        }

        private void query()
        {
            listData();
            if (uiDataGridView已选.DataSource != null && uiDataGridView已选.Rows.Count > 0)
            {
                int 卷数 = uiDataGridView已选.Rows.Count;
                //重量
                decimal 重量 = 0;
                for (int i = 0; i < uiDataGridView已选.Rows.Count; i++)
                {
                    decimal.TryParse(uiDataGridView已选.Rows[i].Cells["CWeight"].Value.ToString(), out decimal a);
                    重量 += a;
                }
                uiTextBox卷数.Text = 卷数.ToString();
                uiTextBox重量.Text = 重量.ToString();
            }
            else
            {
                uiTextBox卷数.Text = "0";
                uiTextBox重量.Text = "0";
            }
        }

        private void listData()
        {
            if (uiRadioButton酸洗.Checked)
            {
                DataTable dt = queryPlanPickling();
                uiDataGridView已选.DataSource = dt;
                uiDataGridView已选.Refresh();
            }

            if (uiRadioButton轧硬.Checked)
            {
                DataTable dt = queryPlanHard();
                uiDataGridView已选.DataSource = dt;
                uiDataGridView已选.Refresh();
            }
            if (uiRadioButton镀锌.Checked)
            {
                DataTable dt = queryPlanZinc();
                uiDataGridView已选.DataSource = dt;
                uiDataGridView已选.Refresh();
            }
            if (uiRadioButton彩涂.Checked)
            {
                DataTable dt = queryPlanPaint();
                uiDataGridView已选.DataSource = dt;
                uiDataGridView已选.Refresh();
            }
            if (uiRadioButton罩退.Checked)
            {
                DataTable dt = queryPlanCoverFire();
                uiDataGridView已选.DataSource = dt;
                uiDataGridView已选.Refresh();
            }
            if (uiRadioButton退火.Checked)
            {
                DataTable dt = queryPlanFire();
                uiDataGridView已选.DataSource = dt;
                uiDataGridView已选.Refresh();
            }
            if (uiRadioButton硅钢.Checked)
            {
                //queryLablePrint("硅钢");
                DataTable dt = queryPlanSi();
                uiDataGridView已选.DataSource = dt;
                uiDataGridView已选.Refresh();
            }
        }

        private DataTable queryPlanFire()
        {
            string sql = @"select [guid], [WorkShopGuid], [WorkShopCode], [DepartGuid], [DepartCode], [WorkLineCode], [WorkLineName], 
                            [WorkDate], [RowNumber], [OriginalSize], [Address], [Meter], [VolumeCode], [VolumeCodeNew], [Weight],
                            [OutputSize], [ProductSize], [NextWorkType], [Remark], [Material], [StorageLocation], [MAC], [CreateID],
                            [Creator], [CreateDate], [ApproverID], [Approver], [ApproverDate], [IsSendToNC], [SendToNCDate], [Customer],
                            [Package], [FaceDone], [SplitDesc] from Bus_PlanFire where 1=1";
            sql += " and CreateDate >= '" + this.uiDatePicker开始日期.Value + "' and CreateDate <= '" + this.uiDatePicker结束日期.Value.AddDays(1) + "'";

            if (!string.IsNullOrEmpty(uiTextBox卷号.Text.Trim()))
            {
                sql += " and VolumeCode like '%" + uiTextBox卷号.Text.Trim() + "%' or VolumeCodeNew like '%" + uiTextBox卷号.Text.Trim() + "%' ";
            }

            if (!string.IsNullOrEmpty(uiComboBox产线.SelectedText) && uiComboBox产线.SelectedText != "全部")
            {
                sql += " and WorkLineName like '%" + uiComboBox产线.SelectedText.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(uiComboBox去向.SelectedText) && uiComboBox去向.SelectedText != "全部")
            {
                sql += " and NextWorkType like '%" + uiComboBox去向.SelectedText + "%'";
            }
            if (!string.IsNullOrEmpty(uiTextBox轧制规格.Text.Trim()))
            {
                sql += " and Originalsize like '%" + uiTextBox轧制规格.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(uiTextBox成品规格.Text.Trim()))
            {
                sql += " and ProductSize like '%" + uiTextBox成品规格.Text.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(uiTextBox客户.Text.Trim()))
            {
                sql += " and Customer like '%" + uiTextBox客户 + "%'";
            }
            sql += "order by CreateDate";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);

            return dt;
        }

        private DataTable queryPlanSi()
        {
            string sql = "select guid, WorkShopGuid, WorkShopCode, DepartGuid, DepartCode, WorkLineCode, WorkLineName, WorkDate, ";
            sql += " RowNumber, Originalsize, Address, VolumeCode, VolumeCodeNew, Weight, OutputSize, ProductSize, NextWorkType, Remark, Material,  ";
            sql += " StorageLocation, MAC, CreateID, Creator, CreateDate,Customer from Bus_PlanSi where 1=1";
            sql += " and CreateDate >= '" + this.uiDatePicker开始日期.Value + "' and CreateDate <= '" + this.uiDatePicker结束日期.Value.AddDays(1) + "'";
            if (!string.IsNullOrEmpty(uiTextBox卷号.Text.Trim()))
            {
                sql += " and VolumeCode like '%" + uiTextBox卷号.Text.Trim() + "%' or VolumeCodeNew like '%" + uiTextBox卷号.Text.Trim() + "%' ";
            }

            if (!string.IsNullOrEmpty(uiComboBox产线.SelectedText) && uiComboBox产线.SelectedText != "全部")
            {
                sql += " and WorkLineName like '%" + uiComboBox产线.SelectedText.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(uiComboBox去向.SelectedText) && uiComboBox去向.SelectedText != "全部")
            {
                sql += " and NextWorkType like '%" + uiComboBox去向.SelectedText + "%'";
            }
            if (!string.IsNullOrEmpty(uiTextBox轧制规格.Text.Trim()))
            {
                sql += " and Originalsize like '%" + uiTextBox轧制规格.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(uiTextBox成品规格.Text.Trim()))
            {
                sql += " and ProductSize like '%" + uiTextBox成品规格.Text.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(uiTextBox客户.Text.Trim()))
            {
                sql += " and Customer like '%" + uiTextBox客户 + "%'";
            }
            sql += "order by CreateDate";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);

            return dt;
        }

        private DataTable queryPlanPickling()
        {
            string sql = "select guid, WorkShopGuid, WorkShopCode, DepartGuid, DepartCode, WorkLineCode, WorkLineName, WorkDate, ";
            sql += " RowNumber, Originalsize, Address, VolumeCode, VolumeCodeNew, Weight, OutputSize, ProductSize, NextWorkType, Remark, Material,  ";
            sql += " StorageLocation, MAC, CreateID, Creator, CreateDate,Customer from Bus_PlanPickling where 1=1";
            sql += " and CreateDate >= '" + this.uiDatePicker开始日期.Value + "' and CreateDate <= '" + this.uiDatePicker结束日期.Value.AddDays(1) + "'";
            if (!string.IsNullOrEmpty(uiTextBox卷号.Text.Trim()))
            {
                sql += " and VolumeCode like '%" + uiTextBox卷号.Text.Trim() + "%' or VolumeCodeNew like '%" + uiTextBox卷号.Text.Trim() + "%' ";
            }

            if (!string.IsNullOrEmpty(uiComboBox产线.SelectedText) && uiComboBox产线.SelectedText != "全部")
            {
                sql += " and WorkLineName like '%" + uiComboBox产线.SelectedText.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(uiComboBox去向.SelectedText) && uiComboBox去向.SelectedText != "全部")
            {
                sql += " and NextWorkType like '%" + uiComboBox去向.SelectedText + "%'";
            }
            if (!string.IsNullOrEmpty(uiTextBox轧制规格.Text.Trim()))
            {
                sql += " and Originalsize like '%" + uiTextBox轧制规格.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(uiTextBox成品规格.Text.Trim()))
            {
                sql += " and ProductSize like '%" + uiTextBox成品规格.Text.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(uiTextBox客户.Text.Trim()))
            {
                sql += " and Customer like '%" + uiTextBox客户 + "%'";
            }
            sql += "order by CreateDate";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);

            return dt;
        }

        private DataTable queryPlanCoverFire()
        {
            string sql = @"select [guid], [WorkShopGuid], [WorkShopCode], [DepartGuid], [DepartCode], [WorkLineCode], [WorkLineName], 
                            [WorkDate], [RowNumber], [OriginalSize], [Address], [Meter], [VolumeCode], [VolumeCodeNew], [Weight],
                            [OutputSize], [ProductSize], [NextWorkType], [Remark], [Material], [StorageLocation], [MAC], [CreateID],
                            [Creator], [CreateDate], [ApproverID], [Approver], [ApproverDate], [IsSendToNC], [SendToNCDate], [Customer],
                            [Package], [FaceDone], [SplitDesc] from Bus_PlanCoverFire where 1=1";
            sql += " and CreateDate >= '" + this.uiDatePicker开始日期.Value + "' and CreateDate <= '" + this.uiDatePicker结束日期.Value.AddDays(1) + "'";

            if (!string.IsNullOrEmpty(uiTextBox卷号.Text.Trim()))
            {
                sql += " and VolumeCode like '%" + uiTextBox卷号.Text.Trim() + "%' or VolumeCodeNew like '%" + uiTextBox卷号.Text.Trim() + "%' ";
            }

            if (!string.IsNullOrEmpty(uiComboBox产线.SelectedText) && uiComboBox产线.SelectedText != "全部")
            {
                sql += " and WorkLineName like '%" + uiComboBox产线.SelectedText.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(uiComboBox去向.SelectedText) && uiComboBox去向.SelectedText != "全部")
            {
                sql += " and NextWorkType like '%" + uiComboBox去向.SelectedText + "%'";
            }
            if (!string.IsNullOrEmpty(uiTextBox轧制规格.Text.Trim()))
            {
                sql += " and Originalsize like '%" + uiTextBox轧制规格.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(uiTextBox成品规格.Text.Trim()))
            {
                sql += " and ProductSize like '%" + uiTextBox成品规格.Text.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(uiTextBox客户.Text.Trim()))
            {
                sql += " and Customer like '%" + uiTextBox客户 + "%'";
            }
            sql += "order by CreateDate";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);

            return dt;
        }

        private DataTable queryPlanHard()
        {
            string sql = @"select [guid], [WorkShopGuid], [WorkShopCode], [DepartGuid], [DepartCode], [WorkLineCode], [WorkLineName], 
                        [WorkDate], [RowNumber], [OriginalSize], [Address], [Meter], [VolumeCode], [VolumeCodeNew], [Weight], [OutputSize], 
                        [ProductSize], [NextWorkType], [Remark], [Material], [StorageLocation], [MAC], [CreateID], [Creator], [CreateDate],
                        [ApproverID], [Approver], [ApproverDate], [IsSendToNC], [SendToNCDate], [Customer] from Bus_PlanHard
                        where 1=1";
            sql += " and CreateDate >= '" + this.uiDatePicker开始日期.Value + "' and CreateDate <= '" + this.uiDatePicker结束日期.Value.AddDays(1) + "'";

            if (!string.IsNullOrEmpty(uiTextBox卷号.Text.Trim()))
            {
                sql += " and VolumeCode like '%" + uiTextBox卷号.Text.Trim() + "%' or VolumeCodeNew like '%" + uiTextBox卷号.Text.Trim() + "%' ";
            }

            if (!string.IsNullOrEmpty(uiComboBox产线.SelectedText) && uiComboBox产线.SelectedText != "全部")
            {
                sql += " and WorkLineName like '%" + uiComboBox产线.SelectedText.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(uiComboBox去向.SelectedText) && uiComboBox去向.SelectedText != "全部")
            {
                sql += " and NextWorkType like '%" + uiComboBox去向.SelectedText + "%'";
            }
            if (!string.IsNullOrEmpty(uiTextBox轧制规格.Text.Trim()))
            {
                sql += " and Originalsize like '%" + uiTextBox轧制规格.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(uiTextBox成品规格.Text.Trim()))
            {
                sql += " and ProductSize like '%" + uiTextBox成品规格.Text.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(uiTextBox客户.Text.Trim()))
            {
                sql += " and Customer like '%" + uiTextBox客户 + "%'";
            }
            sql += "order by CreateDate";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);

            return dt;
        }

        private DataTable queryPlanPaint()
        {
            string sql = @"select [guid], [WorkShopGuid], [WorkShopCode], [DepartGuid], [DepartCode], [WorkLineCode], [WorkLineName],
                        [WorkDate], [RowNumber], [OriginalSize], [Address], [Meter], [VolumeCode], [VolumeCodeNew], [Weight], 
                        [OutputSize], [ProductSize], [NextWorkType], [Remark], [Material], [StorageLocation], [MAC], [CreateID],
                        [Creator], [CreateDate], [ApproverID], [Approver], [ApproverDate], [IsSendToNC], [SendToNCDate], [Customer]
                         from Bus_PlanPaint where 1=1 ";
            sql += " and CreateDate >= '" + this.uiDatePicker开始日期.Value + "' and CreateDate <= '" + this.uiDatePicker结束日期.Value.AddDays(1) + "'";

            if (!string.IsNullOrEmpty(uiTextBox卷号.Text.Trim()))
            {
                sql += " and VolumeCode like '%" + uiTextBox卷号.Text.Trim() + "%' or VolumeCodeNew like '%" + uiTextBox卷号.Text.Trim() + "%' ";
            }

            if (!string.IsNullOrEmpty(uiComboBox产线.SelectedText) && uiComboBox产线.SelectedText != "全部")
            {
                sql += " and WorkLineName like '%" + uiComboBox产线.SelectedText.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(uiComboBox去向.SelectedText) && uiComboBox去向.SelectedText != "全部")
            {
                sql += " and NextWorkType like '%" + uiComboBox去向.SelectedText + "%'";
            }
            if (!string.IsNullOrEmpty(uiTextBox轧制规格.Text.Trim()))
            {
                sql += " and Originalsize like '%" + uiTextBox轧制规格.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(uiTextBox成品规格.Text.Trim()))
            {
                sql += " and ProductSize like '%" + uiTextBox成品规格.Text.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(uiTextBox客户.Text.Trim()))
            {
                sql += " and Customer like '%" + uiTextBox客户 + "%'";
            }
            sql += "order by CreateDate";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);

            return dt;
        }

        private DataTable queryPlanZinc()
        {
            string sql = @"select [guid], [WorkShopGuid], [WorkShopCode], [DepartGuid], [DepartCode], [WorkLineCode], [WorkLineName], 
                            [WorkDate], [RowNumber], [OriginalSize], [Address], [VolumeCode], [VolumeCodeNew], [Weight], [Meter], 
                            [OutputSize], [ProductSize], [NextWorkType], [Remark], [Material], [StorageLocation], [MAC], [CreateID],
                            [Creator], [CreateDate], [ApproverID], [Approver], [ApproverDate], [IsSendToNC], [SendToNCDate], [Customer],
                            [ZincThick], [SplitDesc] from [dbo].[Bus_PlanZinc] where 1=1 ";
            sql += " and CreateDate >= '" + this.uiDatePicker开始日期.Value + "' and CreateDate <= '" + this.uiDatePicker结束日期.Value.AddDays(1) + "'";

            if (!string.IsNullOrEmpty(uiTextBox卷号.Text.Trim()))
            {
                sql += " and VolumeCode like '%" + uiTextBox卷号.Text.Trim() + "%' or VolumeCodeNew like '%" + uiTextBox卷号.Text.Trim() + "%' ";
            }

            if (!string.IsNullOrEmpty(uiComboBox产线.SelectedText) && uiComboBox产线.SelectedText != "全部")
            {
                sql += " and WorkLineName like '%" + uiComboBox产线.SelectedText.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(uiComboBox去向.SelectedText) && uiComboBox去向.SelectedText != "全部")
            {
                sql += " and NextWorkType like '%" + uiComboBox去向.SelectedText + "%'";
            }
            if (!string.IsNullOrEmpty(uiTextBox轧制规格.Text.Trim()))
            {
                sql += " and Originalsize like '%" + uiTextBox轧制规格.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(uiTextBox成品规格.Text.Trim()))
            {
                sql += " and ProductSize like '%" + uiTextBox成品规格.Text.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(uiTextBox客户.Text.Trim()))
            {
                sql += " and Customer like '%" + uiTextBox客户 + "%'";
            }
            sql += "order by CreateDate";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);

            return dt;
        }

        private void uiRadioButton酸洗_CheckedChanged(object sender, EventArgs e)
        {
            if (uiRadioButton酸洗.Checked)
            {
                当前来源选项 = sender as UIRadioButton;
                pub.ListWorkLineAll(pub.全局工厂编号, "", uiRadioButton酸洗.Text, "", uiComboBox产线);
                query();
            }
        }

        private void uiRadioButton轧硬_CheckedChanged(object sender, EventArgs e)
        {
            if (uiRadioButton轧硬.Checked)
            {
                当前来源选项 = sender as UIRadioButton;
                pub.ListWorkLineAll(pub.全局工厂编号, "", uiRadioButton轧硬.Text, "", uiComboBox产线);
                query();
            }
        }

        private void uiRadioButton镀锌_CheckedChanged(object sender, EventArgs e)
        {
            if (uiRadioButton镀锌.Checked)
            {
                当前来源选项 = sender as UIRadioButton;
                pub.ListWorkLineAll(pub.全局工厂编号, "", uiRadioButton镀锌.Text, "", uiComboBox产线);
                query();
            }
        }

        private void uiRadioButton彩涂_CheckedChanged(object sender, EventArgs e)
        {
            if (uiRadioButton彩涂.Checked)
            {
                当前来源选项 = sender as UIRadioButton;
                pub.ListWorkLineAll(pub.全局工厂编号, "", uiRadioButton彩涂.Text, "", uiComboBox产线);
                query();
            }
        }

        private void uiRadioButton退火_CheckedChanged(object sender, EventArgs e)
        {
            if (uiRadioButton退火.Checked)
            {
                当前来源选项 = sender as UIRadioButton;
                pub.ListWorkLineAll(pub.全局工厂编号, "", uiRadioButton退火.Text, "", uiComboBox产线);
                query();
            }
        }

        private void uiRadioButton硅钢_CheckedChanged(object sender, EventArgs e)
        {
            if (uiRadioButton硅钢.Checked)
            {
                当前来源选项 = sender as UIRadioButton;
                pub.ListWorkLineAll(pub.全局工厂编号, "", uiRadioButton硅钢.Text, "", uiComboBox产线);
                query();
            }
        }

        private void uiRadioButton罩退_CheckedChanged(object sender, EventArgs e)
        {
            if (uiRadioButton罩退.Checked)
            {
                当前来源选项 = sender as UIRadioButton;
                pub.ListWorkLineAll(pub.全局工厂编号, "", uiRadioButton罩退.Text, "", uiComboBox产线);
                query();
            }
        }

        private void uiButton删除_Click(object sender, EventArgs e)
        {
            //判断有没有选
            if (当前来源选项 == null || 当前来源选项.Tag == null)
            {
                MessageBox.Show("请选择计划类型");
                return;
            }
            if (MessageBox.Show("确定要删除所选的计划？", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                //如果是酸洗，删除的时候更新在库状态为在库
                if (当前来源选项.Tag.ToString() == "Bus_PlanPickling")
                {
                    int a = 还原原料卷状态();
                    int r = 删除计划();
                    if (r * a > 0)
                    {
                        MessageBox.Show("删除成功");
                        return;
                    }
                    else
                    {
                        MessageBox.Show("删除或更新在库状态失败");
                        return;
                    }
                }
                else
                {
                    int r = 删除计划();
                    if (r > 0)
                    {
                        MessageBox.Show("删除成功");
                        return;
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                        return;
                    }
                }


                query();
            }
        }

        private int 删除计划()
        {
            List<string> guids = new List<string>();
            int r = 0;
            for (int i = 0; i < uiDataGridView已选.Rows.Count; i++)
            {
                if (uiDataGridView已选.Rows[i].Cells["C选择"].Value != null && (bool)uiDataGridView已选.Rows[i].Cells["C选择"].Value)
                {
                    string guid = uiDataGridView已选.Rows[i].Cells["Cguid"].Value.ToString();
                    guids.Add(guid);
                }
            }
            if (guids.Count > 0)
            {
                string guid = string.Join("','", guids.ToArray());
                //删除
                string sql = "delete from " + 当前来源选项.Tag.ToString() + " where guid in ('" + guid + "')";
                r = pub.ExecuteSql(pub.ConnectionString, sql, "删除计划");
            }
            return r;
        }

        private int 还原原料卷状态()
        {
            List<string> VolumeCodes = new List<string>();
            int r = 0;
            for (int i = 0; i < uiDataGridView已选.Rows.Count; i++)
            {
                if (uiDataGridView已选.Rows[i].Cells["C选择"].Value != null && (bool)uiDataGridView已选.Rows[i].Cells["C选择"].Value)
                {
                    string VolumeCode = uiDataGridView已选.Rows[i].Cells["CVolumeCodeNew"].Value.ToString();
                    VolumeCodes.Add(VolumeCode);
                }
            }
            if (VolumeCodes.Count > 0)
            {
                string volumes = string.Join("','", VolumeCodes.ToArray());
                //删除
                string sqlupdate原料卷在库状态 = "update Bus_Volume set IsInStorage = '在库' where VolumeCodeNew in ('" + volumes + "')";
                r = pub.ExecuteSql(pub.ConnectionString, sqlupdate原料卷在库状态, "sqlupdate原料卷在库状态");
            }
            return r;
        }


    }
}
